/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bear.fe.conn2;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
 *
 * @author libear
 */
public class VFile {

    public static final int OPERATION_SAVE = 1;
    public static final int OPERATION_REMOVE = 2;
    public static final int OPERATION_READ = 0;
   
    ID sessionId = null;
    String filePath;   
    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    Set<VFileHistory> history = new TreeSet<VFileHistory>();
    int persistStatus = 1;
    int sessionStatus = 0;
    VFileTable table = null;
    public String getLatestFle(ID id){
        long time = id.startTime;
        for(VFileHistory h:history){
            if(time<h.statusUpdateTime){
                return h.filePath;
            }
        }
        return null;
    } 
    
    public void putStatus2History(String tempPath){
        VFileHistory h = new VFileHistory(tempPath,sessionId.startTime);
        history.add(h);
        
    } 
    
    public int getPersistStatus() {
        return persistStatus;
    }

    public void setPersistStatus(int persistStatus) {
        this.persistStatus = persistStatus;
    }

    public int getSessionStatus() {
        return sessionStatus;
    }

    public void setSessionStatus(int sessionStatus) {
        this.sessionStatus = sessionStatus;
    }
    
    
    public Set<VFileHistory> getHistory() {
        return history;
    }

  

    public VFileTable getTable() {
        return table;
    }

    public void setTable(VFileTable table) {
        this.table = table;
    }


    public VFile(String filePath) {
        this.filePath = filePath;
    }
    
    public VFile(String filePath,ID sessionId) {
        this.filePath = filePath;
        this.sessionId = sessionId;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public ID getSessionId() {
        return sessionId;
    }

    public void setSessionId(ID sessionId) {
        this.sessionId = sessionId;
    }
    
    public void readLock(){
        lock.readLock().lock();
    }
    public void readUnLock(){
        lock.readLock().unlock();
    }
    public void writeLock(){
        lock.writeLock().lock();
    }
    public void writeUnLock(){
        lock.writeLock().unlock();
    }
    public boolean isWriteLock(){
        return lock.writeLock().tryLock();
    }
    
    
}
